package org.eventexample.ws;

import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.eventexample.www.events.Event;
import org.w3c.dom.CharacterData;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;


public class EventsDAO 
{
	public Event[] getAllEvents() 
	{
		Event[] events = null;
		try {
	
		String URLString = "http://events.sjsu.edu/RSSSyndicator.aspx?category=&location=&type=N&starting=4/13/2012&ending=4/19/2012&binary=Y&keywords=";
			
		DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
		URL u = new URL(URLString);

		Document doc = builder.parse(u.openStream());

		NodeList nodes = doc.getElementsByTagName("item");

		int size = nodes.getLength();
		events = new Event[size];
		
		for(int i=0;i<nodes.getLength();i++) 
		{
			Element element = (Element)nodes.item(i);	
			String title =  getElementValue(element,"title");
			String link = getElementValue(element,"link");
			String dateTime = getElementValue(element,"pubDate");
			String startDate = getElementValue(element,"category");
			String description =  getElementValue(element,"description");
			
			SimpleDateFormat dateFormat = new SimpleDateFormat("mm/dd/yyyy"); 
		    Date convertedStartDate = dateFormat.parse(startDate); 
		    System.out.println("Description: " + description);
		    
		    events[i] = new Event();
		    events[i].setEvent_id(i);
		    events[i].setEvent_title(title);
		    events[i].setEvent_link(link);
		    events[i].setEvent_start_date(convertedStartDate);
		    events[i].setEvent_description(description);
		    events[i].setEvent_date_time(dateTime);
		}//for
		}//try
		catch(Exception ex) {
		ex.printStackTrace();
		}
		return events;
		}

	public Event[] getEventsByKeyword(String keyword) 
	{
		Event[] events = null;
		try {
	
		String URLString = "http://events.sjsu.edu/RSSSyndicator.aspx?category=&location=&type=N&starting=4/13/2012&ending=4/19/2012&binary=Y&keywords=" + keyword;
			
		DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
		URL u = new URL(URLString);

		Document doc = builder.parse(u.openStream());

		NodeList nodes = doc.getElementsByTagName("item");

		int size = nodes.getLength();
		events = new Event[size];
		
		for(int i=0;i<nodes.getLength();i++) 
		{
			Element element = (Element)nodes.item(i);	
			String title =  getElementValue(element,"title");
			String link = getElementValue(element,"link");
			String dateTime = getElementValue(element,"pubDate");
			String startDate = getElementValue(element,"category");
			String description =  getElementValue(element,"description");
			
			SimpleDateFormat dateFormat = new SimpleDateFormat("mm/dd/yyyy"); 
		    Date convertedStartDate = dateFormat.parse(startDate); 
		    System.out.println("Description: " + description);
		    
		    events[i] = new Event();
		    events[i].setEvent_id(i);
		    events[i].setEvent_title(title);
		    events[i].setEvent_link(link);
		    events[i].setEvent_start_date(convertedStartDate);
		    events[i].setEvent_description(description);
		    events[i].setEvent_date_time(dateTime);
		}//for
		}//try
		catch(Exception ex) {
		ex.printStackTrace();
		}
		return events;
		}
	
	
	
	public Event[] getEventsByDate(String start, String end) 
	{
		Event[] events = null;
		try {
	
		String URLString = "http://events.sjsu.edu/RSSSyndicator.aspx?category=&location=&type=N&starting="+start+"&ending="+end+"&binary=Y&keywords=";
			
		DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
		URL u = new URL(URLString);

		Document doc = builder.parse(u.openStream());

		NodeList nodes = doc.getElementsByTagName("item");

		int size = nodes.getLength();
		events = new Event[size];
		
		for(int i=0;i<nodes.getLength();i++) 
		{
			Element element = (Element)nodes.item(i);	
			String title =  getElementValue(element,"title");
			String link = getElementValue(element,"link");
			String dateTime = getElementValue(element,"pubDate");
			String startDate = getElementValue(element,"category");
			String description =  getElementValue(element,"description");
			
			SimpleDateFormat dateFormat = new SimpleDateFormat("mm/dd/yyyy"); 
		    Date convertedStartDate = dateFormat.parse(startDate); 
		    System.out.println("Description: " + description);
		    
		    events[i] = new Event();
		    events[i].setEvent_id(i);
		    events[i].setEvent_title(title);
		    events[i].setEvent_link(link);
		    events[i].setEvent_start_date(convertedStartDate);
		    events[i].setEvent_description(description);
		    events[i].setEvent_date_time(dateTime);
		}//for
		}//try
		catch(Exception ex) {
		ex.printStackTrace();
		}
		return events;
		}
	
	
	
	
		protected float getFloat(String value) {
		if(value != null && !value.equals("")) {
		return Float.parseFloat(value);
		}
		return 0;
		}

		private String getCharacterDataFromElement(Element e) {
			try {
			Node child = e.getFirstChild();
			if(child instanceof CharacterData) {
			CharacterData cd = (CharacterData) child;
			return cd.getData();
			}
			}
			catch(Exception ex) {

			}
			return "";
			} //private String getCharacterDataFromElement

		
		protected String getElementValue(Element parent,String label) 
		{
		return getCharacterDataFromElement((Element)parent.getElementsByTagName(label).item(0));
		}	
}